package com.ruoyi.merchant.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.merchant.domain.dto.AddBankCardDTO;
import com.ruoyi.merchant.domain.vo.BankCardDetailVO;
import com.ruoyi.merchant.domain.vo.BankCardVO;
import com.ruoyi.merchant.service.IMerchantBankCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商家银行卡Controller
 */
@RestController
@RequestMapping("/merchant/bankcard")
public class MerchantBankCardController extends BaseController {
    
    @Autowired
    private IMerchantBankCardService bankCardService;
    
    /**
     * 添加银行卡
     */
    @Log(title = "银行卡管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addBankCard(@Validated @RequestBody AddBankCardDTO addDTO) {
        Long cardId = bankCardService.addBankCard(addDTO);
        return AjaxResult.success("添加成功", cardId);
    }
    
    /**
     * 查询银行卡列表
     */
    @GetMapping("/list")
    public AjaxResult getBankCardList() {
        List<BankCardVO> list = bankCardService.getBankCardList();
        return AjaxResult.success(list);
    }
    
    /**
     * 查询银行卡详情
     */
    @GetMapping("/{cardId}")
    public AjaxResult getBankCardDetail(@PathVariable Long cardId) {
        BankCardDetailVO vo = bankCardService.getBankCardDetail(cardId);
        return AjaxResult.success(vo);
    }
    
    /**
     * 设置默认银行卡
     */
    @Log(title = "银行卡管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{cardId}/set-default")
    public AjaxResult setDefaultBankCard(@PathVariable Long cardId) {
        int rows = bankCardService.setDefaultBankCard(cardId);
        return rows > 0 ? AjaxResult.success("设置成功") : AjaxResult.error("设置失败");
    }
    
    /**
     * 删除银行卡
     */
    @Log(title = "银行卡管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{cardId}")
    public AjaxResult deleteBankCard(@PathVariable Long cardId) {
        int rows = bankCardService.deleteBankCard(cardId);
        return rows > 0 ? AjaxResult.success("删除成功") : AjaxResult.error("删除失败");
    }
    
    /**
     * 验证银行卡
     */
    @Log(title = "银行卡管理", businessType = BusinessType.UPDATE)
    @PostMapping("/{cardId}/verify")
    public AjaxResult verifyBankCard(@PathVariable Long cardId) {
        int rows = bankCardService.verifyBankCard(cardId);
        return rows > 0 ? AjaxResult.success("验证成功") : AjaxResult.error("验证失败");
    }
}
